package com.linln.wxapi;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.domain.Membernotice;
import com.linln.modules.member.service.MembernoticeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 用户消息接口
 * @author hct
 *
 */
@Api(value="消息列表")
@RestController
@RequestMapping("/wx/notice/{area}")
public class WxApiNoticeController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MembernoticeService noticeService;

    /**
     * 获取我的消息
     * @param area
     * @return
     */
     @RequestMapping(value = "list",method={RequestMethod.POST,RequestMethod.GET})
     @ResponseBody
     public ResultVo list(@PathVariable String area,Long memberId,Integer type,Integer noticeStatus) {
        
         try {
         	// 创建匹配器，进行动态查询匹配
             ExampleMatcher matcher = ExampleMatcher.matching();
                     //.withMatcher("type_name", match -> match.contains())
             Membernotice notice=new Membernotice();
             notice.setArea(area);
             notice.setIsread(0);
             notice.setType(type);
             MemberInfo member=new MemberInfo();
             member.setId(memberId);
             notice.setMember(member);
             if(noticeStatus!=null) {
             notice.setNoticestatus(noticeStatus);
             }
             // 获取数据列表
             Example<Membernotice> example = Example.of(notice, matcher);
             //Sort sort = new Sort(Sort.Direction.ASC, "num");
             Page<Membernotice> list = noticeService.getPageList(example);
             Map<String,Object> map=new HashMap<String,Object>();
             map.put("list", list.getContent());
             map.put("pageNumber", list.getPageable().getPageNumber()+1);
             map.put("pageSize", list.getPageable().getPageSize());
             return ResultVoUtil.success(map);
         } catch (Exception e) {
             this.logger.error(e.getMessage(), e);
             return ResultVoUtil.error("接口异常"); 
         }
     }
     
     /**
      * 获取我的消息
      * @param area
      * @return
      */

      @RequestMapping(value = "unread",method={RequestMethod.POST,RequestMethod.GET})
      @ResponseBody
      public ResultVo unread(@PathVariable String area,Long memberId) {
         
          try {
          	// 创建匹配器，进行动态查询匹配
              ExampleMatcher matcher = ExampleMatcher.matching();
                      //.withMatcher("type_name", match -> match.contains())
              Membernotice notice=new Membernotice();
              MemberInfo member=new MemberInfo();
              member.setId(memberId);
              notice.setMember(member);
              notice.setIsread(0);
              // 获取数据列表
              Example<Membernotice> example = Example.of(notice, matcher);
              //Sort sort = new Sort(Sort.Direction.ASC, "num");
              long count = noticeService.getUnreadCount(example);
              Map<String,Object> map=new HashMap<String,Object>();
              map.put("count", count);
              return ResultVoUtil.success(map);
          } catch (Exception e) {
              this.logger.error(e.getMessage(), e);
              return ResultVoUtil.error("接口异常"); 
          }
      }
  
      
      @RequestMapping(value = "detail",method={RequestMethod.POST,RequestMethod.GET})
      @ResponseBody
      public ResultVo detail(@PathVariable String area,Long noticeId) {
         
          try {
        	  Membernotice notice=noticeService.getById(noticeId);
        	  noticeService.updateIsread(noticeId);
              Map<String,Object> map=new HashMap<String,Object>();
              map.put("notice", notice);
              return ResultVoUtil.success(map);
          } catch (Exception e) {
              this.logger.error(e.getMessage(), e);
              return ResultVoUtil.error("接口异常"); 
          }
      }
}
